//
//  RouURL.swift
//  RouSwift
//
//  Created by leecong on 2017/8/24.
//  Copyright © 2017年 leecong. All rights reserved.
//

import Foundation

public extension URL{
    
    /// URL with appending query parameters.
    ///
    ///		let url = URL(string: "https://google.com")!
    ///		let param = ["q": "Swifter Swift"]
    ///		url.appendingQueryParameters(params) -> "https://google.com?q=Swifter%20Swift"
    ///
    /// - Parameter parameters: parameters dictionary.
    /// - Returns: URL with appending given query parameters.
    public func appendingQuryParameters(_ parameters :[String:String]) ->URL{
        var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true)!
        var items = urlComponents.queryItems ?? []
        items += parameters.map({URLQueryItem(name: $0, value: $1)})
        urlComponents.queryItems = items
        return urlComponents.url!
    }
    
    public mutating func appendingQueryParameters(_ parameters:[String: String]){
        self = appendingQuryParameters(parameters)
    }
    
    
    
    
    
    

}
